﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kakeibo.View {
    public partial class GridPanel : UserControl, IView {
        public class GridItem {
            public GridItem(long year, long month, long day, string debtor, string sub_debtor, string creditor, string sub_creditor, long amount){
                Date = "" + year + "/" + month + "/" + day;
                Debtor = debtor + " - " + sub_debtor;
                Creditor = creditor + " - " + sub_creditor;
                Amount = (int)amount;
            }
            public string Date { get; set; }
            public string Debtor { get; set; }
            public string Creditor { get; set; }
            public int Amount { get; set; }
        }

        public GridPanel() {
            InitializeComponent();
        }

        public System.Windows.Forms.Control FormControl {
            get { return this; }
        }

        public void SetDataSource(List<GridItem> items) {
            if (items.Count > dataGridView.Rows.Count) {
                dataGridView.Rows.Add(items.Count - dataGridView.Rows.Count);
            }
            while(items.Count < dataGridView.Rows.Count) {
                dataGridView.Rows.RemoveAt(0);
            }

            for (int row = 0; row < items.Count; row++) {
                dataGridView.Rows[row].Cells[0].Value = items[row].Date;
                dataGridView.Rows[row].Cells[1].Value = items[row].Debtor;
                dataGridView.Rows[row].Cells[2].Value = items[row].Creditor;
                dataGridView.Rows[row].Cells[3].Value = items[row].Amount;
            }
        }
    }
}
